const fs = require('fs');
const exec = require('./exec');
module.exports = {
	summary:'请求拦截',
	*beforeSendResponse(request,response) {
		if(request.requestOptions.hostname === 'apilb.stepn.com') {
			try{
				console.log('---------------------------------------------------')
				console.log(request.url,'------',request.requestOptions.headers.cookie);
				if(request.url.indexOf("login") > -1) {
					const res = response.response.header['set-cookie'];
					console.log('获取到新的session：',res);
					fs.writeFileSync('session',res,'utf8');
					exec.closeApp()
				}else{
					const res = response.response.body.toString();
					if(res) {
						const result = JSON.parse(res);
						const session = request.requestOptions.headers.cookie;
						console.log(`重新设置session：${session}`);
						if (result && result.code === 0 && session) {
							fs.writeFileSync('session',session,'utf8');
							exec.closeApp()
						}
					}
				}			
			}catch(e) {
				console.log('error',e);
			}
		}
		return null
	}
}